package v1

import (
	"strconv"

	"gitee.com/cyanbleeze/go-image-management/internal/service"
	"gitee.com/cyanbleeze/go-image-management/pkg/app"
	"gitee.com/cyanbleeze/go-image-management/pkg/errcode"
	"github.com/gin-gonic/gin"
)

type FavoirtePicture struct{}

func NewFavoirtePicture() FavoirtePicture {
	return FavoirtePicture{}
}

func (fp FavoirtePicture) Get(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Param("id"))
	res := app.NewResponse(ctx)
	if err != nil {
		res.ToErrorResponse(errcode.InvalidParams.WithDetails(err.Error()))
		return
	}
	svc := service.New(ctx)
	d, err2 := svc.GetFavoritePicture(&service.FavoritePictureRequest{ID: uint(id)})
	if err2 != nil {
		res.ToErrorResponse(errcode.ErrorGetFavoritePictureFail.WithDetails(err.Error()))
		return
	}
	res.ToResponse(d)
}
func (fp FavoirtePicture) List(ctx *gin.Context) {
	params := service.FavoritePictureListRequest{}
	res := app.NewResponse(ctx)
	valid, errs := app.BindAndValid(ctx, &params)
	if !valid {
		res.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}
	pager := app.Pager{
		Page:     app.GetPage(ctx),
		PageSize: app.GetPageSize(ctx),
	}

	svc := service.New(ctx)
	detailList, count, err := svc.GetFavoritePictureList(&params, &pager)
	if err != nil {
		res.ToErrorResponse(errcode.ErrorGetFavoritePictureListFail.WithDetails(err.Error()))
		return
	}
	res.ToResponseList(detailList, count)
}
func (fp FavoirtePicture) Create(ctx *gin.Context) {
	params := service.CreateFavoritePictureRequest{}
	res := app.NewResponse(ctx)
	valid, errs := app.BindAndValid(ctx, &params)
	if !valid {
		res.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}
	svc := service.New(ctx)
	err := svc.CreateFavoritePicture(&params)
	if err != nil {
		res.ToErrorResponse(errcode.ErrorCreateFavoritePictureFail.WithDetails(err.Error()))
		return
	}
	res.ToResponse(gin.H{})
}
func (fp FavoirtePicture) Update(ctx *gin.Context) {
	res := app.NewResponse(ctx)
	id, IDErr := strconv.Atoi(ctx.Param("id"))
	if IDErr != nil {
		res.ToErrorResponse(errcode.InvalidParams.WithDetails(IDErr.Error()))
		return
	}
	params := service.UpdateFavoritePictureRequest{ID: uint(id)}
	valid, errs := app.BindAndValid(ctx, &params)
	if !valid {
		res.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}
	svc := service.New(ctx)
	err := svc.UpdateFavoritePicture(&params)
	if err != nil {
		res.ToErrorResponse(errcode.ErrorUpdateFavoritePictureFail.WithDetails(err.Error()))
		return
	}
	res.ToResponse(gin.H{})
}
func (fp FavoirtePicture) Delete(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Param("id"))
	res := app.NewResponse(ctx)
	if err != nil {
		res.ToErrorResponse(errcode.InvalidParams.WithDetails(err.Error()))
		return
	}
	svc := service.New(ctx)
	err2 := svc.DeleteFavoritePicture(&service.DeleteFavoritePictureRequest{ID: uint(id)})
	if err2 != nil {
		res.ToErrorResponse(errcode.ErrorDeleteFavoritePictureFail.WithDetails(err.Error()))
		return
	}
	res.ToResponse(gin.H{})
}
